package com.lm.activiti.boot.cases.thread;

import lombok.SneakyThrows;
import org.junit.Test;

import java.util.concurrent.*;

/**
 * @author lm_jcf
 * @date 2021/5/26 16:22
 */
public class ExecutorCase {

    public static void main(String[] args) throws InterruptedException {


        for (int i = 0; i < 10; i++) {

            ExecutorHolder holder = new ExecutorHolder();

            holder.add();

            Thread.sleep(3 * 1000);

            System.out.println(((ThreadPoolExecutor) holder.executorService).getActiveCount());

            holder.printThread();
        }
    }
}

class ExecutorHolder{

    public ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    /*static {
        executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    }*/

    public void add(){
        new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < 100; i++) {

                    executorService.submit(new Runnable() {
                        @SneakyThrows
                        @Override
                        public void run() {
                            Thread.sleep(500);
                        }
                    });
                }
            }
        }).start();
    }

    public void printThread(){
        System.out.println(((ThreadPoolExecutor) executorService).getActiveCount());
    }

}
